package com.apple1.blogservices.impl;

import com.apple1.blogdao.LevelMapper;
import com.apple1.blogentity.Level;
import com.apple1.blogentity.LevelExample;
import com.apple1.blogentity.vo.LevelVo;
import com.apple1.blogservices.LevelService;
import com.apple1.commonutil.BeanMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 *
 * @author chencheng
 */
@Service
public class LevelServiceImpl implements LevelService {
    @Autowired
    LevelMapper levelMapper;
    /**
     * 方法名: 添加等级
     * 处理逻辑:
     * note(注意):
     * @param level
     * @return int
     */
    @Override
    public int addLevel(LevelVo level) {
        if (level == null || level.getLevelName() == null){
            return 0;
        }
        Level mapBean = BeanMapUtil.getMapBean(level, Level.class);
        mapBean.setTableId(null);

        return levelMapper.insertSelective(mapBean);
    }

    /**
     * note:等用户服务需要时在修改
     * @param level_id
     * @return
     */
    @Override
    public int delLevelById(Integer level_id) {
        throw new RuntimeException("未实现的操作");
    }


    /**
     * 方法名: 更新等级
     * 处理逻辑:
     * note(注意):
     * @param level
     * @return int
     */
    @Override
    public int updateLevel(LevelVo level) {
        if (level == null || level.getLevelName() == null || level.getTableId() == null){
            return 0;
        }
        Level mapBean = BeanMapUtil.getMapBean(level, Level.class);
        LevelExample levelExample = new LevelExample();
        LevelExample.Criteria criteria = levelExample.createCriteria();
        criteria.andTableIdEqualTo(level.getTableId());
        mapBean.setTableId(null);

        return levelMapper.updateByExampleSelective(mapBean,levelExample);
    }

    /**
     * 方法名: 根据等级id查询等级信息
     * 处理逻辑:
     * note(注意):
     * @param level_id
     * @return com.apple1.blogentity.vo.LevelVo
     */
    @Override
    public LevelVo selectLevelById(Integer level_id) {
        if (level_id == null){
            return null;
        }
        LevelExample levelExample = new LevelExample();
        LevelExample.Criteria criteria = levelExample.createCriteria();
        criteria.andTableIdEqualTo(level_id);


        List<Level> levels = levelMapper.selectByExample(levelExample);

        if (levels == null || levels.isEmpty()){
            return null;
        }

        Level level = levels.get(0);


        return BeanMapUtil.getMapBean(level,LevelVo.class);
    }
}
